isshow = false;
zm.ready(function() {
    zm('.remove-answer').bind("click", function() {

        parent = zm(this).parent().parent();
        var isDeny = zm(".header-form").attr("response");
        if (isDeny === "false") {
            zm(this).parent().parent().remove();
        }
        else {
              zm.Boxy.alert( "Bạn không thể xóa câu hỏi này!","Lỗi");
        }
    });

    // raise edit question event
    
    zm('.edit-answer').bind("click", function() {
        parent = zm(this).parent().parent();
        var edit = parent.attr("isedit");
        if (edit === "true" && isshow ===false) {
            
            isEdit = true;
            editquestion();
            zm('.question').addClass('isedit');
            parent.addClass("isedit");
            isshow =true;
        }
        else {
            //  alert("Bạn không thể sửa câu hỏi này!");
              zm.Boxy.alert("Bạn không thể sửa câu hỏi này!","Lỗi");
        }
    });

    // done question
    zm(".done-question").bind("click", function() {        
        if (!isEdit) {
            var value = addquestion_Edit_Page();
            if (value === "")
                return;
            zm(".form-question").append(value);
            isEdit = false;
        }
        else {
            var value = editquestion_Edit_Page();
            if (value === "")
                return; 
            parent.html(value);
            isEdit = false;
            parent.removeClass("isedit");
        }

        zm(".question").remove();

    });
    zm("#submit_edit_page").click(function() {
        // find title and decription form
        var idSurvey = zm('.header-form').attr("idsurvey");
        var title = zm('.header-form input').val();
        var descripton = zm('.header-form textarea').val();
        var share = zm(".share").attr("checked");
        var start = zm('#date-start').val().split("/");
        var end = zm('#date-end').val().split("/");
        var sday = start[0];
        var smonth = start[1];
        var syear = start[2];
        var eday = end[0];
        var emonth = end[1];
        var eyear = end[2];
        var point = zm('#point').val();
        var beginDate = new Date();
        beginDate.setDate(sday);
        beginDate.setMonth(smonth - 1);
        beginDate.setFullYear(syear);
        var endDate = new Date();
        endDate.setDate(eday);
        endDate.setMonth(emonth - 1);
        endDate.setFullYear(eyear);
        var benginDay = parseInt(beginDate.getTime() / 1000);
        var endDay = parseInt(endDate.getTime() / 1000);
        var survey = new Survey(title, descripton, benginDay, endDay, point, share);
        // find question form
        var index = 1;
        zm('.question-answer').each(function() {

            var title = zm(this).find('.question-title').text();
            var helpText = zm(this).find('.question-helptext').text();
            var type = zm(this).find('.answer').attr("typeanswer");
            var require = zm(this).find('.require').attr("requirement");

            var question = new Question(index, title, helpText, type, require);
            if (type === singleselection || type === multichoice) {

                zm(this).find('.answer .answer-option').each(function() {
                    var opption = zm(this).find('label').text();
                    question.addChoices(opption);
                });
            }
            survey.addToQuestions(question);
            index++;
        });
        var error = checkSurvey(survey);
        if(error!==""){
              zm.Boxy.alert(error,"Lỗi nhập liệu");
        }
        var JSONSurvey = zm.serialize(survey);      
        ajaxEditSurvey(idSurvey, JSONSurvey);

    });

    zm(".add-item").click(function() {
        if (isshow === false) {
            if(isEdit===false){
                zm(".question").find("input").each(function() {
                    zm(this).val("");
                });
                parent = zm('.form-question');
                parent.after(getQuestionForm());
                zm('.question').addClass('boder-style');
                AddQuestionEvent();
                
            }
            else{
                zm('.question').after(getQuestionForm());
            }
            isshow = true;
        }
    });
    zm(".form-question").addClass("boder-style");

});
function addquestion_Edit_Page() {

    var require = zm('input[name="require"]').attr("checked");
    var isDeny = zm(".header-form").attr("response");
    if (isDeny === "true" && require) {
          zm.Boxy.alert( "Bạn không thể tạo một câu hỏi bắt buộc!","Cảnh báo");
        return "";
    }
    var num;
    if (isEdit)
        num = parent.find(".question-span").text();
    else {
        num = zm(".form-question").find(".question-answer").length + 1;
        num = "Câu " + num;
    }
    var title = zm('input[name="title"]').attr("value");
    var help = zm('input[name="help-text"]').attr("value");
    var type = zm('#choice-answer').val();
    var question = '<div class="question-answer" >';
    if (title === null || title === "") {
        zm.Boxy.alert("Hãy nhập nội dung câu hỏi!", "Lỗi nhập liệu");
        return "";
    }
    var temp = true;
    question += '<div class="edit non-display"><button class="remove-answer" >Xóa</button>';

    question += '<button class="edit-answer" >Sửa</button></div><span class="question-span">' + num + '</span><span class="question-title">' + title + "</span>";
    if (require === true) {
        question += '<span  class="require" requirement="true">(*)</span>';
    }
    else {
        question += '<span class="require" requirement="false"></span>';
    }

    question += '<p class="question-helptext">' + help + '</p>';
    if (type === singleline) {
        question += '<div class="answer" typeanswer="' + singleline + '">';

        question += '<input type="text" size="50"/>';
    }
    if (type === multiline) {
        question += '<div class="answer" typeanswer="' + multiline + '">';
        question += '<textarea cols="50" ></textarea>';

    }
    if (type === multichoice) {
        question += '<div class="answer" typeanswer="' + multichoice + '">';
        zm(".option").each(function() {
            var txt = zm(this).find('input[type="text"]').val();
             if (txt === null || txt === "") {
              temp = false;
              return;
            }
            question += '  <div class="answer-option">'
                    + '<input type="checkbox" ><span>' + txt + '</span>'
                    + '</div>';
        });
    }
    if (type === singleselection) {
        question += '<div class="answer" typeanswer="' + singleselection + '">';
        var name = radioName++;
        zm(".option").each(function() {
            var txt = zm(this).find('input[type="text"]').val();
             if (txt === null || txt === "") {
              temp = false;
              return;
            }
            question += '<div class="answer-option" >'
                    + '<input type="radio" name="' + name.toString() + '"><span>' + txt + '</span>'
                    + '</div>';
        });

    }
     if(temp===false){
         zm.Boxy.alert("Hãy nhập nội dung lựa chọn!", "Lỗi nhập liệu");
         return "";
     }
    question += '</div></div>';
    return question;

}

function editquestion_Edit_Page() {
    zm(".form-question").addClass("boder-style");
    var require = zm('input[name="require"]').attr("checked");
    var isDeny = zm(".header-form").attr("response");
    if (isDeny === "true" && require) {
        zm.Boxy.alert( "Bạn không thể tạo một câu hỏi bắt buộc!","Cảnh báo");
        return "";
    }
    var num;
    if (isEdit)
        num = parent.find(".question-span").text();
    else {
        num = zm(".form-question").find(".question-answer").length + 1;
        num = "Câu " + num;
    }
    var title = zm('input[name="title"]').attr("value");
    var help = zm('input[name="help-text"]').attr("value");
    var type = zm('#choice-answer').val();
    var question = '<div class="question-answer">';
     if (title === null || title === "") {
        zm.Boxy.alert("Hãy nhập nội dung câu hỏi!", "Lỗi nhập liệu");
        return "";
    }
    var temp = true;
    question += '<div class="edit non-display"><button class="remove-answer  " >Xóa</button>';

    question += '<button class="edit-answer" >Sửa</button></div><span class="question-span">' + num + '</span><span class="question-title">' + title + "</span>";
    if (require === true) {
        question += '<span  class="require" requirement="true">(*)</span>';
    }
    else {
        question += '<span class="require" requirement="false"></span>';
    }

    question += '<p class="question-helptext">' + help + '</p>';
    if (type === singleline) {
        question += '<div class="answer" typeanswer="' + singleline + '">';

        question += '<input type="text" size="50"/>';
    }
    if (type === multiline) {
        question += '<div class="answer" typeanswer="' + multiline + '">';
        question += '<textarea cols="50" ></textarea>';

    }
    if (type === multichoice) {
        question += '<div class="answer" typeanswer="' + multichoice + '">';
        zm(".option").each(function() {
            var txt = zm(this).find('input[type="text"]').val();
            if (txt === null || txt === "") {
              temp = false;
              return;
            }
            question += '  <div class="answer-option">'
                    + '<input type="checkbox" ><span>' + txt + '</span>'
                    + '</div>';
        });
    }
    if (type === singleselection) {
        question += '<div class="answer" typeanswer="' + singleselection + '">';
        var name = radioName++;
        zm(".option").each(function() {
            var txt = zm(this).find('input[type="text"]').val();
            if (txt === null || txt === "") {
              temp = false;
              return;
            }
            question += '<div class="answer-option" >'
                    + '<input type="radio" name="' + name.toString() + '"><span>' + txt + '</span>'
                    + '</div>';
        });

    }
     if(temp===false){
         zm.Boxy.alert("Hãy nhập nội dung lựa chọn!", "Lỗi nhập liệu");
         return "";
     }
    question += '</div></div>';
    return question;

}

function AddQuestionEvent() {
    // select combobox event
    zm("#choice-answer").change(function() {
        var type = zm(this).val();
        if (type === singleline) {
            zm(".content-answer").html(getText());
        }
        if (type === multiline) {
            zm(".content-answer").html(getParagraph());
        }
        if (type === singleselection || type === multichoice) {
            zm(".content-answer").html(getMultiChoice());
            // and event for button add
            zm(".add-option").bind("click", function() {
                zm(".add-option").before(getChoice());
                //removeButton();
                zm(".remove").unbindAll();
                zm(".remove").bind("click", function() {
                    zm(this).parent().remove();
                });
            });
        }
    });
 //cancel button
    zm(".cancel-question").click(function() {
          zm(".question").remove();
          parent.show();
          isshow=false;
    });
    // done question
    zm(".done-question").bind("click", function() {
        if (!isEdit) {
            var value = addquestion_Edit_Page();
            if (value === "")
                return;
            zm(".form-question").append(value);
            isEdit = false;
        }
        else {
            var value = editquestion_Edit_Page();
            if (value === "")
                return;
            parent.remove();
            zm('.question').after(value);
            isEdit = false;

        }
        isshow = false;

        zm(".question").remove();


        // raise delete question event
        zm('.remove-answer').unbindAll("click");
        zm('.remove-answer').bind("click", function() {
            zm(this).parent().parent().remove();
            var i = 1;
            zm(".form-question").find(".question-answer").each(function() {
                zm(this).find(".question-span").text("Câu " + i + ": ");
                i++;
            });

        });

        // raise edit question event
        zm('.edit-answer').unbindAll("click");
        zm('.edit-answer').bind("click", function() {
            if (isshow === false) {
                isEdit = true;
                parent = zm(this).parent().parent();
                editquestion();
                zm('.question').addClass('isedit');
                isshow = true;
            }
        });
        zm(".question").remove();

    });

}

function editquestion() {

    var title = parent.find(".question-title").text();
    var help = parent.find(".question-helptext").text();
    var type = parent.find(".answer").attr("typeanswer");
    var require = parent.find(".require").attr("requirement");
    var value = new Array();
    var i = 0;
    parent.find(".answer-option").each(function() {
        var temp = zm(this).find("span").text();
        value[i++] = temp;
    });
    showeditquestion(title, help, type, require, value);
}

function showeditquestion(title, help, type, require, value) {
    parent.after(getQuestionForm());
    AddQuestionEvent();
    var temp = false;
    if (require === "true")
        temp = true;
    zm('input[name="require"]').attr("checked", temp);
    zm('input[name="title"]').attr("value", title);
    zm('input[name="help-text"]').attr("value", help);
    var temp = "";
    zm('#choice-answer').val(type);
       temp+='<span class="bcl1">&nbsp;</span>';
    temp+='<span class="bcr1">';
    for (var i = 0; i < value.length; i++) {
        temp += '<div class="option"><input type="radio" name="0" disabled=""><input type="text" size="50" value="' + value[i] + '"/><button class="remove">Xóa</button><br></div>';
    }
    if (type === singleselection || type === multichoice) {
        temp += '<button class="add-option button-zm1">Thêm</button>';
    }
     temp+='</span><div class="clr"><div>';
    zm(".content-answer").html(temp);
    // and event for button add
    zm(".add-option").bind("click", function() {
        zm(".add-option").before(getChoice());
        //removeButton();
        zm(".remove").unbindAll();
        zm(".remove").bind("click", function() {
            zm(this).parent().remove();
            //removeButton();
        });

    });

    zm(".remove").unbindAll();
    zm(".remove").bind("click", function() {
        zm(this).parent().remove();
        //removeButton();
    });
    parent.hide();
}